507961
@@ -38,6 +38,7 @@
import javax.servlet.http.HttpServletRequest;
 
 import org.wildfly.clustering.ee.Batch;
 import org.wildfly.clustering.ee.BatchContext;
+import org.wildfly.clustering.ee.Batcher;
 import org.wildfly.clustering.web.session.Session;
 import org.wildfly.clustering.web.session.SessionManager;
 import org.wildfly.clustering.web.undertow.logging.UndertowClusteringLogger;
@@ -76,9 +77,16 @@
public class DistributableSession implements io.undertow.server.session.Session
     public void requestDone(HttpServerExchange exchange) {
         Session<LocalSessionContext> session = this.entry.getKey();
         if (session.isValid()) {
-            try (BatchContext context = this.manager.getSessionManager().getBatcher().resumeBatch(this.batch)) {
-                session.close();
-                this.batch.close();
+            Batcher<Batch> batcher = this.manager.getSessionManager().getBatcher();
+            try (BatchContext context = batcher.resumeBatch(this.batch)) {
+                // If batch was discarded, close it
+                if (this.batch.getState() == Batch.State.DISCARDED) {
+                    this.batch.close();
+                }
+                // If batch is closed, close session in a new batch
+                try (Batch batch = (this.batch.getState() == Batch.State.CLOSED) ? batcher.createBatch() : this.batch) {
+                    session.close();
+                }
             } catch (Throwable e) {
                 // Don't propagate exceptions at the stage, since response was already committed
                 UndertowClusteringLogger.ROOT_LOGGER.warn(e.getLocalizedMessage(), e);
